﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PdxDemo.MasterDetailDemo
{
    public class ViewModel : ViewmodelBase
    {
        public ViewModel()
        {
            Masters = new List<MasterPOCO>();

            var master = new MasterPOCO() { Name = "Splinter" };
            master.Apprentices = new ApprenticePOCO[] {
                new ApprenticePOCO() { Name = "Leonardo", Rank = "Blue" },
                new ApprenticePOCO() { Name = "Raphael", Rank = "Red" },
                new ApprenticePOCO() { Name = "Michelangelo", Rank = "Orange" },
                new ApprenticePOCO() { Name = "Donatello", Rank = "Purple" },
            }.ToList();
            master.Ranks = master.Apprentices.Select(a => a.Rank).Distinct().ToList();
            Masters.Add(master);

            master = new MasterPOCO() { Name = "Obi-Wan" };
            master.Apprentices = new ApprenticePOCO[] {
                new ApprenticePOCO() { Name = "Anakin", Rank = "Padawan" },
                new ApprenticePOCO() { Name = "Luke", Rank = "Jedi Knight" },
            }.ToList();
            master.Ranks = master.Apprentices.Select(a => a.Rank).Distinct().ToList();
            master.Ranks.Add("Jedi Master");
            Masters.Add(master);

            SelectedMaster = Masters[0];
        }

        public List<MasterPOCO> Masters { get; set; }

        MasterPOCO selectedMaster;
        public MasterPOCO SelectedMaster
        {
            get { return selectedMaster; }
            set
            {
                if (selectedMaster != value)
                {
                    selectedMaster = value;
                    if (!value.Apprentices.Contains(SelectedApprentice))
                    {
                        if (value.Apprentices.Count > 0)
                        {
                            SelectedApprentice = value.Apprentices[0];
                        }
                        else
                        {
                            SelectedApprentice = null;
                        }
                    }

                    OnPropertyChanged("SelectedMaster");
                }
            }
        }

        ApprenticePOCO selectedApprentice;
        public ApprenticePOCO SelectedApprentice
        {
            get { return selectedApprentice; }
            set
            {
                if (selectedApprentice != value)
                {
                    selectedApprentice = value;
                    OnPropertyChanged("SelectedApprentice");
                }
            }
        }

        public void MoveUp()
        {
            var index = SelectedMaster.Apprentices.IndexOf(SelectedApprentice) - 1;
            if (index >= 0)
            {
                SelectedApprentice = SelectedMaster.Apprentices[index];
                OnPropertyChanged("dummy");
            }
        }

        public void MoveDown()
        {
            var index = SelectedMaster.Apprentices.IndexOf(SelectedApprentice) + 1;
            if (index < SelectedMaster.Apprentices.Count)
            {
                SelectedApprentice = SelectedMaster.Apprentices[index];
                OnPropertyChanged("dummy");
            }
        }
    }
}
